package com.huilian.tool.common.utils;
import com.alibaba.fastjson.JSON;
import com.huilian.tool.common.config.WeixinConfig;
import com.huilian.tool.common.config.WxOpenConfig;
import com.huilian.tool.weixin.enums.MessageTypeEnum;
import com.huilian.tool.weixin.vo.Image;
import com.huilian.tool.weixin.vo.WeixinMessage;
import com.huilian.tool.weixin.vo.WeixinMessageImage;
import com.huilian.tool.weixin.vo.WeixinMessageText;
import com.thoughtworks.xstream.XStream;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * @authou 少凡
 * @Date 2019-04-12 16:44
 */
public class MessageUtil {

    /**
     * 将文本对象转xml
     *
     * @param weixinMessage
     * @return
     */
    public static String textMessageToXml(WeixinMessage weixinMessage) {
        XStream xStream = new XStream();
        xStream.alias("xml", weixinMessage.getClass());
        return xStream.toXML(weixinMessage);
    }


    /**
     * xml转为map集合
     * @param request
     * @return
     * @throws IOException
     * @throws DocumentException
     * @throws AesException
     */
    public static Map<String, String> xmlToMap(HttpServletRequest request, WeixinConfig weixinConfig) throws IOException, DocumentException, AesException {

        String msgSignature = request.getParameter("msg_signature");
        String timeStamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String encryptMsg = HuiLianHttpUtil.streamToString(request);

        WXBizMsgCrypt pc = new WXBizMsgCrypt(weixinConfig.getEncodingAesKey());
        //获得解密消息
        String result = pc.decryptMsg(weixinConfig.getAppId(), weixinConfig.getToken(), msgSignature, timeStamp, nonce, encryptMsg);
        Map<String, String> map = new HashMap<>(6);
        //将解密后的消息转为xml
        Document doc = DocumentHelper.parseText(result);
        Element root = doc.getRootElement();
        List<Element> list = root.elements();
        for (Element e : list) {
            map.put(e.getName(), e.getText());
        }
        return map;
    }

    /**
     * 开放平台xml转为map集合
     *
     * @param request
     * @return
     * @throws IOException
     * @throws DocumentException
     * @throws AesException
     */
    public static Map<String, String> xmlToMapTicket(HttpServletRequest request, WxOpenConfig wxOpenConfig) throws IOException, DocumentException, AesException {

        String msgSignature = request.getParameter("msg_signature");
        String timeStamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String encryptMsg =  HuiLianHttpUtil.streamToString(request);
        WXBizMsgCrypt pc = new WXBizMsgCrypt(wxOpenConfig.getComponentToken(), wxOpenConfig.getComponentAesKey(), wxOpenConfig.getComponentAppId());
        //获得解密消息
        String result = pc.decryptTicket(msgSignature, timeStamp, nonce, encryptMsg);
        Map<String, String> map = new HashMap<>(6);
        //将解密后的消息转为xml
        Document doc = DocumentHelper.parseText(result);
        Element root = doc.getRootElement();
        List<Element> list = root.elements();
        for (Element e : list) {
            map.put(e.getName(), e.getText());
        }
        System.out.println("最终获得数据--------------");
        System.out.println(JSON.toJSONString(map));
        return map;
    }



    /**
     * 发送文本消息
     *
     * @param fromUserName
     * @param toUserName
     * @param content
     * @return
     */
    public static String sendTestMessage(String fromUserName, String toUserName, String content) {
        WeixinMessageText text = new WeixinMessageText();
        text.setFromUserName(toUserName);
        text.setToUserName(fromUserName);
        text.setMsgType(MessageTypeEnum.TEXT.getCode());
        text.setCreateTime(String.valueOf(new Date().getTime()));
        text.setContent(content);
        return MessageUtil.textMessageToXml(text);
    }


    public static String sendWxMessageImage(String fromUserName, String toUserName, String mediaId) {
        WeixinMessageImage image = new WeixinMessageImage();
        image.setFromUserName(toUserName);
        image.setToUserName(fromUserName);
        image.setMsgType(MessageTypeEnum.IMAGE.getCode());
        image.setCreateTime(String.valueOf(new Date().getTime()));
        image.setImage(new Image(mediaId));
        return MessageUtil.textMessageToXml(image);
    }

    public static String transferCustomerService(String fromUserName, String toUserName) {
        WeixinMessageText text = new WeixinMessageText();
        text.setFromUserName(toUserName);
        text.setToUserName(fromUserName);
        text.setMsgType("transfer_customer_service");
        text.setCreateTime(String.valueOf(new Date().getTime()));
//        text.getTransInfo().setKfAccount("kf2001@gh_acecaa842e16");

        String msg = MessageUtil.textMessageToXml(text);
        return msg;
    }

}
